//配置路由的地方
import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store';
//使用插件
Vue.use(VueRouter);
//引用路由组件
import Home from '@/views/Home'
import Search from '@/views/Search'
import Login from '@/views/Login'
import Register from '@/views/Register'
import Detail from '@/views/Detail'
import AddCartSuccess from '@/views/AddCartSuccess'
import ShopCart from '@/views/ShopCart';

let originPush = VueRouter.prototype.push;
//重写push/replace
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        originPush.call(this, location, resolve, reject);
    } else {
        originPush.call(this, location, () => { }, () => { });
    }
}

//配置路由
let router = new VueRouter({
    routes: [
        {
            path: '/',
            component: () => import('@/views/Home'),
            meta: {
                show: true
            }
        },
        {
            path: '/home',
            component: Home,
            meta: {
                show: true
            }
        },
        {
            path: '/search/:keyword?',
            component: Search,
            meta: {
                show: true
            },
            name: 'search'
        },
        {
            path: '/login',
            component: Login
        },
        {
            path: '/register',
            component: Register
        },
        {
            path: '/detail/:goodItemId',
            component: Detail,
            meta: {
                show: true
            }
        },
        {
            name: 'addcartsuccess',
            path: '/addcartsuccess',
            component: AddCartSuccess,
            meta: {
                show: true
            }
        },
        {
            name: 'shopCart',
            path: '/shopcart',
            component: ShopCart,
            meta: {
                show: true
            }
        }
    ],
    //滚动行为
    scrollBehavior(to, from, savedPosition) {
        return { y: 0 };
    }
})
//导航守卫
router.beforeEach(async (to, from, next) => {
    // ...
    let token = store.state.user.token;
    let name = store.state.user.userInfo.name;
    console.log(token);
    next();
    //用户登录了
    if (token) {
        //已经登录而且还想去登录------不行
        if (to.path == '/login' || to.path == '/register') {
            next('/home');
        } else {
            //已经登陆了,访问的是非登录与注册  
            if (name) {
                //登录了且拥有用户信息放行
                next();
            } else {
                //登陆了且没有用户信息
                try {
                    //在路由跳转之前获取用户信息且放行
                    await store.dispatch('getUserInfo');
                    next();
                } catch (error) {
                    //token失效重新登录  未写
                    // console.log(error);
                }
            }
        }
    } else {
        //未登录：不能去交易相关、不能去支付相关【pay|paysuccess】、不能去个人中心
        //未登录去上面这些路由-----登录
        let toPath = to.path;
        if (toPath.indexOf('/trade') != -1 || toPath.indexOf('/pay') != -1 || toPath.indexOf('/center') != -1) {
            //把未登录的时候向去而没有去成的信息，存储于地址栏中【路由】
            next('/login?redirect=' + toPath);
        } else {
            next();
        }
    }

})

export default router;